package com.silverhaze.diamond.client.utils;

import com.silverhaze.diamond.client.common.AuroraConstants;
import com.silverhaze.diamond.common.entity.Customer;
import com.silverhaze.diamond.common.entity.Service;
import com.silverhaze.diamond.client.ui.UIConstants;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Author: Ivan Babanin
 * © Lizard Software, 2007 y.
 */
public class ProjectUtils {

    public static final int WARNING = 0;
    public static final int SEVERE = 1;

    public static final int SEC_IN_MIN = 60;
    public static final int MIN_IN_HOUR = 60;

    public static final int MLS_IN_SEC = 1000;
    public static final int MLS_IN_MIN = MLS_IN_SEC * SEC_IN_MIN;
    public static final int MLS_IN_HOUR = MLS_IN_MIN * MIN_IN_HOUR;

    public static String getTime() {
        return AuroraConstants.TIME_EXTENDED_FORMAT.format(new Date());
    }

    public static List<String> parseStringIntoList(String input) {
        List<String> result = new ArrayList<String>();

        String[] tempArray = input.split(",");
        for (String aTempArray : tempArray) {
            result.add(aTempArray.trim());
        }

        return result;
    }

    public static String formatListIntoString(List<String> list) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < (list.size() - 1); i++) {
            result.append(list.get(i)).append(", ");
        }
        result.append(list.get(list.size()));

        return result.toString();
    }

    public static Pattern buildPattern(String patternString) {
        StringBuilder result = new StringBuilder();

        patternString = patternString.replace("+", "\\+");
        patternString = patternString.replaceAll("\\*", ".*");

        result.append("^").append(patternString).append("$");
        return Pattern.compile(result.toString());
    }

    public static String generateToolTipByService(Service s) {
        StringBuilder builder = new StringBuilder();

        builder.append("<html><h2>   Сервис #").append(s.getId()).append(" ").append(s.isServiceComplete() ?
                "<font color=green>(завершен)</font>" : "<font color=red>(в ремонте)</font>").append("</h2><ul>");
        builder.append("<li><b>Покупатель: </b>").append(s.getOwner().getExtendedName()).append("  <br>");
        builder.append("<li><b>Принятая техника: </b>").append(s.getTechNames()).append("");
        builder.append("<li><b>Изделие (модель): </b>").append(s.getModelName()).append("  <br>");
        builder.append("<li><b>Тех. название модели: </b>").append(s.getTechModelName()).append("  <br>");
        builder.append("<li><b>Состояние пломбы: </b>").append(s.getStampState()).append("  <br>");
        builder.append("<li><b>Серийный номер: </b>").append(s.getSerialNumber()).append("  <br>");
        builder.append("<li><b>Дата производства: </b>").append(UIConstants.DATE.format(s.getDateProduce())).append("  <br>");
        builder.append("<li><b>Гарантийный номер: </b>").append(s.getGuarantyId()).append("  <br>");
        builder.append("<li><b>Целостность: </b>").append(s.getCompleteness()).append("  <br>");
//        builder.append("<li><b>Customer comment: </b>").append(s.getOwnerDefect()).append("  <br>");
//        builder.append("<li><b>Master comment: </b>").append(s.getMasterDefect()).append("  <br>");

        builder.append("</ul>");

        if (s.isServiceComplete()) {
            builder.append("<ul>");

            builder.append("<li><b>Тип сервиса: </b>").append(s.getServiceType());
            builder.append("<li><b>Дата завершения сервиса: </b>").append(UIConstants.DATE.format(s.getServiceCompleteDate()));
            builder.append("<li><b>Кол-во установленных деталей: </b>").append(s.getUsedDetailes().size());
            builder.append("<li><b>Кол-во неисправных деталей: </b>").append(s.getBrokenDetailes().size());
            builder.append("<li><b>Стоимость: </b>").append(s.getCost());

            builder.append("</ul>");
        }

        builder.append("</html>");

        return builder.toString().replaceAll(" ", "&nbsp;");
    }

    public static String generateToolTipByCustomer(Customer customer) {
        return "";
    }
}
